/*
package com.lightjet.macross.base.feign.api.service.auth.impl;

import com.lightjet.macross.base.feign.api.service.auth.AuthTokenService;

import com.lightjet.macross.common.util.RedisUtil;
import lombok.extern.java.Log;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;

@Service
@Log
public class AuthTokenServiceImpl implements AuthTokenService {

    @Resource
    private RedisUtil redisUtil;

    @Override
    public String GetAdminToken(String clientid,String username) {
        String redisKey = "simpleauth:"+clientid+"-"+username;
        String convertedToken= "";
        log.info("尝试从Redis获取key为【"+redisKey+"】的Token");
//首先从缓存中取出 真正的 OAUTH2 的Token
        if(redisUtil.hasKey(redisKey))
        {
            String[] tokenAndExp = redisUtil.get(redisKey).toString().split("@");
            //如果存在redis里的 Token 过期时间存在 （token@exptime ）
            if(tokenAndExp.length>1)
            {
                //如果该token 已过期
                if((new Date()).getTime() > Long.parseLong(tokenAndExp[1]))
                {
                    log.info("从Redis获取的Token已过期");
                    //重新获取OAUTH Token 并保存Redis
                    convertedToken = converToken(clientinfoMap,host);
                }
                else//若未过期 则直接取该Token
                {

                    convertedToken = tokenAndExp[0];
                    log.info("从Redis获取的Token【"+convertedToken+"】未过期：");
                }
            }
            else
            {
                //重新获取OAUTH Token 并保存Redis
                convertedToken = converToken(clientinfoMap,host);
            }
        }
        else
        {
            //重新获取OAUTH Token 并保存Redis
            convertedToken = converToken(clientinfoMap,host);
        }
    }

    private String converToken(HashMap<String,String> exchangeTokenMap, String host)
    {
        log.info("重新从Auth服务进行密码认证");
        MultiValueMap<String ,String> parms = new LinkedMultiValueMap<>();
        parms.add("password","802sastcasc!");
        parms.add("username","0000");
        parms.add("client_id",exchangeTokenMap.get(CLAIM_CLEINTID));
        parms.add("client_secret",exchangeTokenMap.get(CLAIM_CLEINTSECRET));
        parms.add("scope","all");
        parms.add("grant_type","password");
        HashMap resp =  new RestTemplate().postForObject(host+AUTH_TOKEN_PATH,parms, HashMap.class);
        String token =resp.get("token_type").toString() +" "+ resp.get("access_token").toString();
        log.info("密码认证成功："+token);
        long expirein = Long.parseLong(resp.get("expires_in").toString());
        redisUtil.set(REDIS_KEY+exchangeTokenMap.get(CLAIM_CLEINTID)+"-"+exchangeTokenMap.get(CLAIM_USERNAME),token +"@"+ (expirein*1000 + new Date().getTime()) );
        return token;
    }
}
*/
